<!--
 * @Descripttion: 招商管理-意向客户管理 list
 * @version: 
 * @Author: lehaixia
 * @Date: 2023-04-19 11:33:17
 * @LastEditors: lehaixia
 * @LastEditTime: 2023-08-14 13:22:50
-->
<template>
  <div>
    <List ref="qmpList" v-model:finished="finished" :reset-fun="resetData" :load="load" :refresh="load">
      <Item
        v-for="(item, i) in list"
        :key="i"
        :item-data="item"
        @edit="toEdit(item)"
        @detail="toDetail(item)"
        @stop="onStop(item)"
        @allocation="onAllocation(item)"
        :dictData="dictData"
      />
    </List>
  </div>
</template>
<script lang="ts">
  import { ref, reactive, toRefs, defineComponent, watch } from 'vue'
  import { useRouter } from 'vue-router'
  import List from '@/components/List/index.vue'
  import Item from './Item.vue'
  import { geList, detailItem } from '@/apis/attractInvestment/potentialCustomer'
  import { getDictByPatchType } from '@/apis/dataDict'
  import { queryMdmServiceCustDocDefList } from '@/apis/common'

  interface datasModle {
    list: any[]
    finished: boolean
    rowData: any
    dictData: any
  }
  export default defineComponent({
    name: 'PotentialCustomerList',
    components: { List, Item },
    props: {
      searchValue: {
        type: String,
        default: ''
      },
      type: {
        type: String,
        default: ''
      }
    },
    setup(props, ctx) {
      const qmpList = ref()
      const $router = useRouter()
      const datas = reactive<datasModle>({
        list: [],
        finished: false,
        rowData: '',
        dictData: {}
      })
      const load = async (page: number, rows: number) => {
        const filtersRaw: object[] = []

        if (props.searchValue) {
          filtersRaw.push({
            id: '',
            property: '[customerName,customerPhone,managerName]',
            operator: 'OR LIKE',
            value: props.searchValue
          })
        }

        if (props.type === 'tobe_allocate') {
          // 待分配查询
          filtersRaw.push({ id: 'followUpStage', property: 'followUpStage', value: 'A', operator: '=' })
        }
        const paramsData = {
          page,
          rows,
          sort: 'lastUpdateTime',
          order: 'desc',
          filtersRaw,
          paramsRaw: {},
          sortersRaw: [],
          pageFlag: true
        }
        return geList(paramsData).then((res: any) => {
          const resData = res.data
          const list = resData.records
          if (page === 1) {
            datas.list = list
          } else {
            datas.list = datas.list.concat(list)
          }
          if (resData.total <= datas.list.length) {
            datas.finished = true
          }
        })
      }
      const resetData = () => {
        datas.list = []
      }
      const resetPageData = () => {
        qmpList.value.resetPageData()
      }

      // 客户意向编辑
      const toEdit = (row: any) => {
        $router.push({
          path: '/potentialCustomer/edit',
          query: {
            id: row.id
          }
        })
      }
      // 客户意向详情
      const toDetail = (row: any) => {
        $router.push({
          path: '/potentialCustomer/detail',
          query: {
            id: row.id
          }
        })
      }

      // 终止
      const onStop = (row: any) => {
        console.log('终止')
        // datas.showTermination = true
        // datas.rowData = row
        ctx.emit('stop', row)
      }
      // 分配
      const onAllocation = (row: any) => {
        // console.log('分配')
        ctx.emit('allocation', row)
      }

      // 获取数据字典
      const getDictData = async () => {
        const dictTypeCodes = ['CTM_INTEND_CUS_FOLLOW_UP_PROGRESS', 'CTM_TOBE_CUSTOMER_TYPE', 'BIZ_TYPE']
        const dictData = await getDictByPatchType({ dictTypeCodes })
        // 业务类型集合（取mdm主数据）
        const BIZ_TYPE = await queryMdmServiceCustDocDefList('QY037')
        if (BIZ_TYPE.length > 0) {
          dictData.BIZ_TYPE = BIZ_TYPE.map((v: any) => {
            return {
              dictCode: v.code,
              dictName: v.name
            }
          })
        }
        datas.dictData = dictData
      }

      // 刷新单个列表数据
      const refreshItem = (id: string) => {
        const flag = datas.list.some((v) => v.id === id)
        if (!flag) return

        detailItem(id).then((res: any) => {
          const d = res.data
          if (d && d.id) {
            datas.list = datas.list.map((item: any) => {
              if (item.id === id) {
                item = { ...item, ...res.data }
              }
              return item
            })
          }
        })
      }

      getDictData()

      watch(
        () => props.searchValue,
        () => {
          resetPageData()
        }
      )
      return {
        ...toRefs(datas),
        load,
        resetData,
        qmpList,
        resetPageData,
        toEdit,
        toDetail,
        onStop,
        onAllocation,
        refreshItem
      }
    }
  })
</script>
<style lang="less" scoped></style>
